<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>大事件-文章</title>
    <link rel="stylesheet" type="text/css" href="css/reset.css">
    <link rel="stylesheet" href="css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="css/main.css">
</head>

<body>
    <div class="header">
        <a href="#" class="logo fl"><img src="images/logo.png" alt="logo"></a>
        <div class="search_form fl">
            <input type="text" class="search_txt" placeholder="黑洞照片">
            <button class="search_btn"><i class="fa fa-search"></i></button>
        </div>
        <div class="link_info fr">
            <a href="#"><i class="fa fa-angle-right"></i>&nbsp;&nbsp;网站地图</a><br>
            <a href="#"><i class="fa fa-angle-right"></i>&nbsp;&nbsp;招聘信息</a>
        </div>
    </div>

    <div class="menu_con">
        <div class="menu">
            <div class="menu_collapse fl">
                <a href="javascript:;" class="level_one"><i class="fa fa-list-ul"></i>全部分类</a>
                <ul class="level_two">
                    <li class="up"></li>
                    <li><a href="#"></a></li>
                    <li><a href="#"></a></li>
                    <li><a href="#"></a></li>
                    <li><a href="#"></a></li>
                    <li><a href="#"></a></li>
                    <li><a href="#"></a></li>
                </ul>
            </div>
            <ul class="left_menu fl">
                <li><a href="#"></a></li>
                <li><a href="#"></a></li>
                <li><a href="#"></a></li>
                <li><a href="#"></a></li>
                <li><a href="#"></a></li>
                <li><a href="#"></a></li>
            </ul>
            <ul class="right_menu fr">
                <li><a href="#">科技专题</a></li>
                <li><a href="#">财经专题</a></li>
            </ul>
        </div>
    </div>

    <div class="main_con clearfix">
        <div class="left_con setfr">
            <div class="breadcrumb">
                当前位置： <a href="#">首页</a> &gt; <a href="#">科技</a> &gt; 文章详情
            </div>
            <h3 class="article_title"></h3>
            <div class="article_info"></div>
            <div class="article_con">


                <p></p>


                <p></p>


                <p></p>


                <p></p>


                <p></p>


            </div>
            <div class="article_links">
                上一篇： <a href="#" class='prev'></a><br>
                下一篇： <a href="#" class='next'></a>
            </div>

            <form action="" class="comment_form">
                <div class="form_group">
                    <label>用户名：</label>
                    <input type="text" placeholder="请输入用户名" class="comment_name">
                </div>
                <div class="form_group">
                    <label>评论内容：</label>
                    <textarea placeholder="请发表您的评论" class="comment_input"></textarea>
                </div>
                <div class="form_group">
                    <input type="submit" name="" value="评 论" class="comment_sub">
                </div>
            </form>

            <div class="comment_count">
                4条评论
            </div>

            <div class="comment_list_con">
                <div class="comment_detail_list">
                    <div class="person_pic fl"></div>
                    <div class="name_time fl"><b></b><span></span></div>
                    <div class="comment_text fl">
                        
                    </div>
                </div>
                <div class="comment_detail_list">
                    <div class="person_pic fl"></div>
                    <div class="name_time fl"><b></b><span></span></div>
                    <div class="comment_text fl">
                        
                    </div>
                </div>
                <div class="comment_detail_list">
                    <div class="person_pic fl"></div>
                    <div class="name_time fl"><b></b><span></span></div>
                    <div class="comment_text fl">
                        
                    </div>
                </div>
                <div class="comment_detail_list">
                    <div class="person_pic fl"></div>
                    <div class="name_time fl"><b></b><span></span></div>
                    <div class="comment_text fl">
                        
                    </div>
                </div>
            </div>
        </div>
        <div class="right_con setfl">
            <div class="common_wrap">
                <h3><i class="fa fa-bar-chart"></i> 一周热门排行</h3>
                <ul class="content_list">
                    <li><span class="first">1</span><a href="#"></a></li>
                    <li><span class="second">2</span><a href="#"></a></li>
                    <li><span class="third">3</span><a href="#"></a></li>
                    <li><span>4</span><a href="#"></a></li>
                    <li><span>5</span><a href="#"></a></li>
                    <li><span>6</span><a href="#"></a></li>
                    <li><span>7</span><a href="#"></a></li>
                </ul>
            </div>
            <div class="common_wrap">
                <h3><i class="fa fa-commenting-o"></i> 最新评论</h3>
                <ul class="content_list comment_list">
                    <li>
                        <span></span>
                        <b><em></em> </b>
                        <strong></strong>
                    </li>
                    <li>
                        <span></span>
                        <b><em></em> </b>
                        <strong></strong>
                    </li>
                    <li>
                        <span></span>
                        <b><em></em> </b>
                        <strong></strong>
                    </li>
                    <li>
                        <span></span>
                        <b><em></em> </b>
                        <strong></strong>
                    </li>

                </ul>
            </div>
            <div class="common_wrap">
                <h3><i class="fa fa-bullseye"></i> 焦点关注</h3>
                <ul class="content_list guanzhu_list">
                    <li><a href="#"></a></li>
                    <li><a href="#"></a></li>
                    <li><a href="#"></a></li>
                    <li><a href="#"></a></li>
                    <li><a href="#"></a></li>
                    <li><a href="#"></a></li>
                    <li><a href="#"></a></li>
                </ul>
            </div>
        </div>
    </div>

    <div class="footer clearfix">
        <div class="foot_link">
            <a href="#">关于我们</a>
            <span>|</span>
            <a href="#">联系我们</a>
            <span>|</span>
            <a href="#">招聘人才</a>
            <span>|</span>
            <a href="#">友情链接</a>
        </div>
        <p>CopyRight © 2019 深圳大事件信息技术股份有限公司 All Rights Reserved</p>
        <p>电话：0755-****888 粤ICP备*******8号</p>
    </div>

    <!-- 引入模板引擎js文件 -->
    <script src="./js/template-web.js"></script>
    <script src="./js/jquery-1.12.4.min.js"></script>
    <script src="./js/http.js"></script>

</body>
<!-- 文章热门排行模板 -->
<script type="text/html" id="tpl-rank">
    {{each data}}
    <li>
        {{if $index == 0}}
            <span class="first">
        {{else if $index == 1}}
            <span class="second">
        {{else if $index == 2}}
            <span class="third">
        {{else}}
            <span>
        {{/if}}
            {{$index + 1}}</span><a href="./article.html?id={{$value.id}}">{{$value.title}}</a>
    </li>
    {{/each}}
</script>


<!-- 最新评论模板 -->
<script type="text/html" id='tpl-latest-comment'>
    {{each data}}
    <li>
        <span>{{$value.author[0]}}</span>
        <b><em>{{$value.author}}</em> {{$value.date}} {{$value.time}} 说:</b>
        <strong>{{$value.intro}}</strong>
    </li>
    {{/each}}
</script>

<!-- 焦点关注模板 -->
<script type="text/html" id='tpl-attention'>
    {{each data}}
    <li><a href="./article.html?id='{{$value.id}}'">{{$value.intro}}</a></li>
    {{/each}}
</script>

<!-- 全部分类数据 -->
<script type="text/html" id='tpl-category'>
    {{each data}}
    <li><a href="./list.html?categoryId={{$value.id}}">{{$value.name}}</a></li>
    {{/each}}
</script>

<!-- 文章评论列表模板 -->
<script type="text/html" id="tpl-comment">
    {{each data v}}
    <div class="comment_detail_list">
        <div class="person_pic fl">{{v.author[0]}}</div>
        <div class="name_time fl"><b>{{v.author}}</b><span>{{v.date}}&nbsp;{{v.time}}</span></div>
        <div class="comment_text fl">
            {{v.content}}
        </div>
    </div>
    {{/each}}
</script>




</html>

<script>
    $(function () {
        // 1.获得跳转到文章列表页面时携带的文章id值
        let articleId = window.location.search.split('=')[1];
        // console.log(articleId);

        // 2.根据文章id发起ajax请求，获得文章详情数据
        $.ajax({
            type: 'get',
            url: BigNew.index_article,
            data: {
                id: articleId
            },
            success: function (backData) {
                // console.log(backData);
                if (backData.code == 200) {
                    // 根据返回的数据渲染到页面结构
                    let data = backData.data;
                    $('.breadcrumb a').eq(1).text(data.category);
                    $('.article_title').text(data.title);
                    $('.article_info').html(`
                        ${data.author} 发布于 ${data.date}&nbsp;&nbsp;&nbsp;分裂: ${data.category}&nbsp;&nbsp;
                        阅读: (${data.read})&nbsp;&nbsp;&nbsp;评论: (${data.comments})
                    `)
                    $('.article_con').html(data.content)

                    // 设置上下一篇选项
                    if (data.prev) {
                        // 存在上一篇
                        $('.article_links .prev').text(data.prev.title).attr('href',
                            './article.html?id=' + data.prev.id)
                    } else {
                        // 已经是第一篇，不存在上一篇
                        $('.article_links .prev').text('已经是第一篇，没有上一篇').attr('href', '#')
                    };
                    if (data.next) {
                        // 存在下一篇
                        $('.article_links .next').text(data.next.title).attr('href',
                            './article.html?id=' + data.next.id)
                    } else {
                        // 已经是最后一篇，不存在下一篇
                        $('.article_links .next').text('已经是最后一篇，没有下一篇').attr('href', '#')
                    }
                }
            }
        })

        // 3.根据文章id发起ajax请求，获得评论列表数据，渲染页面结构
        $.ajax({
            type: 'get',
            url: BigNew.index_get_comment,
            data: {
                articleId: articleId
            },
            success: function (backData) {
                // console.log(backData);
                if (backData.code == 200) {
                    // 结合模板引擎，生成标签字符串
                    let htmlStr = template('tpl-comment', backData)
                    $('.comment_list_con').html(htmlStr)

                    // 修改评论的数量值
                    $('.comment_count').text(backData.data.length + ' 条评论')
                }
            }
        })


        // 4.给评论按钮注册点击事件，发起ajax请求
        $('.comment_sub').on('click', function (e) {
            e.preventDefault(); // 阻止默认效果（刷新页面）
            // 获得当前输入的评论内容
            let author = $('.comment_name').val().trim();
            let content = $('.comment_input').val().trim();
            // 评论内容非空判断
            if (author == '' || content == '') {
                return
            }

            // 根据获得的数据发起ajax请求
            $.ajax({
                type: 'post',
                url: BigNew.index_post_comment,
                data: {
                    author:author,
                    content:content,
                    articleId:articleId
                },
                success: function (backData) {
                    // console.log(backData);
                    if(backData.code == 201){
                        alert('评论成功 等待审核！！！');
                        $('.comment_name').val('');
                        $('.comment_input').val('');
                    }
                }
            })
        })

        let arr = [{
                url: BigNew.index_rank,
                tplId: 'tpl-rank',
                className: '.content_list'
            },
            {
                url: BigNew.index_latest_comment,
                tplId: 'tpl-latest-comment',
                className: '.comment_list'
            },
            {
                url: BigNew.index_attention,
                tplId: 'tpl-attention',
                className: '.guanzhu_list'
            },
            {
                url: BigNew.index_category,
                tplId: 'tpl-category',
                className: '.level_two,.left_menu',
                fun: function (className, htmlStr) {
                    let class1 = className.split(',')[0];
                    $(className).html(htmlStr)

                    // 当前的容器需要添加一个头部li标签
                    let class2 = className.split(',')[1];
                    $(className).html('<li class="up"></li>' + htmlStr)
                }
            }
        ]

        for (let i = 0; i < arr.length; i++) {
            loadData(arr[i])
        };

        // 7.给搜索按钮注册点击事件
        $('.search_btn').on('click', function () {
            // 获得搜索关键字
            let text = $('.search_txt').val().trim();
            if (text == '') {
                return;
            }

            // 携带搜索关键字跳转到文章列表页面
            window.location = './list.html?key=' + text;
        })



    })
</script>